package com.winit.common.util;

import com.alibaba.fastjson.JSONObject;
import com.winit.core.ci.vcs.SVN;
import com.winit.core.configuration.ConfigRepository;
import com.winit.core.host.shell.ShellerContainer;
import com.winit.core.host.tailer.TailerContainer;
import com.winit.project.core.bean.PackageTypeEnum;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

public class Constants {

    //	websocket中保存登陆用户名key
    public final static String WEBSOCKET_SESSION_USERNAME_INDEX = "websocket_session_username_index";
    //	websocket中保存tail线程id key
    public final static String WEBSOCKET_SESSION_THREADID_INDEX = "websocket_session_threadid_index";
    //	tail线程id前缀
    public final static String TAIL_THREAD_INDEX = "TT";

    public final static TailerContainer TAILER_CONTAINER = new TailerContainer();

    public final static ShellerContainer SHELLER_CONTAINER = new ShellerContainer();

    public final static String SHELL_PATH = StringUtils.isEmpty(ConfigUtil.getValue("shellPath")) ? Thread.currentThread().getContextClassLoader().getResource("").getPath() : ConfigUtil.getValue("shellPath");

    public final static String SSH_LOGIN_USER = ConfigUtil.getValue("ssh_login_user");
    public final static String SSH_KEY_FILE = ConfigUtil.getValue("ssh_key_file");
    public final static String SVN_USER_NAME = ConfigUtil.getValue("svn_user_name");

    //用户角色
    public final static String USER_ROLE_ADMIN = "admin";//管理员
    public final static String USER_ROLE_DEV = "dev";//开发
    public final static String USER_ROLE_QA = "qa";//测试
    public final static String USER_ROLE_OPS = "ops";//运维

    //发布计划模版
    public final static String RELEASE_TEMPLATE_IS_IN_USE = "0";//可用
    public final static String RELEASE_TEMPLATE_IS_UN_USE = "1";//不可用

    //工程发布方式
    public final static String RELEASE_TYPE_WAR = PackageTypeEnum.MAVEN_WAR.getDesc();//war包发布
    public final static String RELEASE_TYPE_JAR = PackageTypeEnum.MAVEN_JAR.getDesc();//jar包发布
    public final static String RELEASE_TYPE_STATIC = PackageTypeEnum.PHP.getDesc();//php发布

    //工程环境终端分组
    public final static String PROJECT_ENV_TERMINAL_GTOUP_A = "A";
    public final static String PROJECT_ENV_TERMINAL_GTOUP_B = "B";
    public final static String PROJECT_ENV_TERMINAL_GTOUP_ALL = "ALL";

    //工程版本提交测试状态
    public final static String PROJECT_EDITION_TO_TEST = "0";//已提交测试
    public final static String PROJECT_EDITION_NOT_TO_TEST = "1";//未提交测试

    //工程版本是否被保护
    public final static String PROJECT_EDITION_IS_PROTECTED = "0";//被保护
    public final static String PROJECT_EDITION_NOT_IS_PROTECTED = "1";//未被保护

    //工程环境是否有haproxy
    public final static String PROJECT_ENV_USE_HAPROXY = "0";//使用haproxy
    public final static String PROJECT_ENV_NOT_USE_HAPROXY = "1";//不使用haproxy

    //工程版本测试状态
    public final static String PROJECT_EDITION_TEST_STATUS_PASS = "0";//测试通过
    public final static String PROJECT_EDITION_TEST_STATUS_NOT_PASS = "1";//测试不通过
    public final static String PROJECT_EDITION_TEST_STATUS_NOT_TEST = "2";//未测试

    //跳板机推送状态
    public final static String JUMPSERVER_PUSH_STATUS_SUCCESS = "0";//推送成功
    public final static String JUMPSERVER_PUSH_STATUS_FAIL = "1";//推送失败
    public final static String JUMPSERVER_PUSH_STATUS_ON = "2";//推送中
    public final static String JUMPSERVER_PUSH_STATUS_STOP = "3";//推送未开始

    public final static String PUSH_THREAD_INDEX = "PUSH";

    //跳板机等级
    public final static String JUMPSERVER_LEVELL_1 = "1";
    public final static String JUMPSERVER_LEVELL_2 = "2";

    public final static String JUMPSERVER_PUSH_FROM_DEFULT = "beetle本机";

    public final static String[] fileTypes = {"environment.properties", "password.properties", "logback.xml", "log4j2.xml"};
    public final static String[] SVNRoles = {"DEV", "QA", "OPS"};

    //0 虚拟机环境  1 非虚拟机环境
    public final static String PROJECT_ENV_IS_VM = "0";
    public final static String PROJECT_ENV_NOT_IS_VM = "1";

    //虚拟机
    public static final String VM_JOB_LOG_DIR = ConfigUtil.getValue(ConfigUtil.ROOT_PATH) + "Beetle/logs/shell/";
    public static final String VM_JOB_LOG_FILE_PREFIX = "log-";
    public static final String VM_JOB_LOG_FILE_SUFFIX = ".log";
    public static final String VMS_DIR = ConfigUtil.getValue(ConfigUtil.ROOT_PATH) + "Beetle/vm/vms/";
    public static final String VM_TEMPLATES_DIR = ConfigUtil.getValue(ConfigUtil.ROOT_PATH) + "Beetle/vm/templates/";
    public static final String VM_TEMPLATES_SUFFIX = ".json";
    public static final String VM_TEMPLATE_FILE_NAME = "VMTemplate.json";
    public static final String VM_TEMPLATE_FILE_PATH = VM_TEMPLATES_DIR + VM_TEMPLATE_FILE_NAME;
    public static final String VM_FILE_SUFFIX = ".json";
    public static final String VM_QUEUE_NAME_FORMAT = "vm-%s";

    public static String getVMQueueNameByName(String vmName) {
        return String.format(VM_QUEUE_NAME_FORMAT, vmName);
    }

    //虚拟机描述文件
    public static final String VM_DESC_DIR = ConfigUtil.getValue(ConfigUtil.ROOT_PATH) + "Beetle/vm/vms/";
    public static final String VM_DESC_FILE_SUFFIX = ".desc";

    //获取oracle初始化特殊参数
    public static String getOracleParamsByName(String name) {
        Map<String, Object> map = JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "oracleInitParams.json", Map.class);
        return map.get(name) == null ? "" : map.get(name).toString();
    }

    public static final Map<String, List> adFolderMap() {
        return JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "adFolderMap.json", Map.class);
    }

    public static final Map<String, List> oracleFolderMap() {
        return JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "oracleFolderMap.json", Map.class);
    }

    public static final Map<String, List> mysqlFolderMap() {
        return JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "mysqlFolderMap.json", Map.class);
    }

    public static final Map<String, List> cassandraFolderMap() {
        return JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "cassandraFolderMap.json", Map.class);
    }

    public final static Integer VM_PHP_AD_LIST_COUNT = StringUtils.isEmpty(ConfigUtil.getValue("vm_php_ad_list_count")) ? 3 : Integer.parseInt(ConfigUtil.getValue("vm_php_ad_list_count"));

    public final static Map<String, List<JSONObject>> ROLE_POWER_MAP = JSONFileUtil.getFileJsonForBean(Constants.SHELL_PATH + "rolePower.json", Map.class);
    public final static Map<String, List<String>> DEFAULT_DATA = JSONFileUtil.getFileJsonForBean(Constants.SHELL_PATH + "defaultData.json", Map.class);

    public static final String IGNORE_RESOURCE_FOLDER = ConfigUtil.getValue("ignore_resource_folder");

    public static final String CONFIG_REPOSITORY_PATH = ConfigUtil.getValue("rootPath") + "Beetle/svnResource/config";

    public static final ConfigRepository CONFIG_REPOSITORY = new ConfigRepository(Constants.CONFIG_REPOSITORY_PATH);

    public static final SVN CONFIG_REPOSITORY_SVN = new SVN(ConfigUtil.getValue("env_config_check_svn_path"), Constants.CONFIG_REPOSITORY_PATH);

    public static final Integer SSH_CONNECT_TIMEOUT = StringUtils.isEmpty(ConfigUtil.getValue("ssh_server_connect_timeout")) ?
            10000 : Integer.parseInt(ConfigUtil.getValue("ssh_server_connect_timeout"));

    public static final String BEETLE_AUTH_URL = ConfigUtil.getValue("beetle_auth_url");
    public static final String BEETLE_PLAN_URL = ConfigUtil.getValue("beetle_plan_url");

    public static final String RELEASE_BAK_DIR = ConfigUtil.getValue("release_bak_dir");
    public static final String RELEASE_PREPARE_TEMP_DIR = ConfigUtil.getValue("release_prepare_temp_dir");

}
